package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.heliang.dataaccess.bean.InfoVersionBean;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.dao.InfoVersionDao;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

public class InfoVersionDaoImpl extends BaseDao implements InfoVersionDao {
    private String tbName;
    
    private Context context;
    
    public InfoVersionDaoImpl(Context context) {
        super(context);
        this.context = context;
    }

    public List<InfoVersionBean> getInfoVersionBeansByIds(Integer[] idArrays) {
        // TODO Auto-generated method stub
        return null;
    }

    public InfoVersionBean getInfoVersionBeanById(int catId) throws DaoException {
        List<InfoVersionBean> infoVersionBeans = new ArrayList<InfoVersionBean>();
        InfoVersionBean infoVersionBean = new InfoVersionBean();
        try {
            infoVersionBeans =  finalDb.findAllByWhere(InfoVersionBean.class, " CATID = "+catId+" ");
            if (infoVersionBeans.size() >0 ) {
                return infoVersionBeans.get(0);
            } else {
                infoVersionBean.setId(0);
                infoVersionBean.setCatId(catId);
                infoVersionBean.setVer(0);
                finalDb.save(infoVersionBean);
                System.out.println(finalDb.findAll(InfoVersionBean.class).get(0));
                
                return infoVersionBean;
            }
        } catch (Exception e) {
             ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "InfoVersionDaoImpl.getInfoVersionBeanById()" + "根据资讯类别查询资讯信息版本失败！");
            return null;
        } 
    }

    public boolean updateInfoVersionBean(InfoVersionBean infoVersionBean) throws DaoException {
        List<InfoVersionBean> infoVersionbeans = new ArrayList<InfoVersionBean>();
       InfoVersionBean newInfoVersion = new InfoVersionBean();
        try {
            infoVersionbeans = finalDb.findAllByWhere( InfoVersionBean.class, "  CATID = "+infoVersionBean.getCatId()+" ");
            newInfoVersion = infoVersionbeans.get(0);
            
            if (newInfoVersion == null) {
                finalDb.save(infoVersionBean);
            } else {
                newInfoVersion.setVer(infoVersionBean.getVer());
                finalDb.update(newInfoVersion, " CATID ="+infoVersionBean.getCatId()+" ");
            }
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_UPDATE_ERROR_003, "InfoVersionDaoImpl.updateInfoVersionBean()" + "更新资讯信息类别版本失败！");
            return false;
        }
    }

    public boolean deleteInfoVersionBeanByIds(Integer[] idArray) throws DaoException {
        try {
            StringBuffer strWhere = new StringBuffer();
            strWhere.append(" CATID IN( ");
            for (int i = 0; i < idArray.length; i++) {
                if (i == idArray.length - 1) {
                    strWhere.append(idArray[i]);
                } else {
                    strWhere.append(idArray[i] + ",");
                }
            }
            strWhere.append(")");

            finalDb.deleteByWhere(InfoVersionBean.class, strWhere.toString());
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "InfoVersionDaoImpl.deleteInfoVersionBeanByIds()" + "根据IDS删除资讯类别版本失败！");
            return false;
        }
    }

}
